package org.mclll.xwangplugin.town.command.executor;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.mclll.xwangplugin.town.database.domain.TownDO;
import org.mclll.xwangplugin.town.database.domain.TownPlayerDO;
import org.mclll.xwangplugin.town.database.mapper.manage.TownManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownPlayerManage;
import org.mclll.xwangplugin.town.enums.PlayerTownIdentity;

import java.util.List;

import static org.mclll.xwangplugin.town.config.TownContext.*;

/**
 * @author: geduo
 * @date: 2023/9/23 19:28
 */
public class TownListExecutor {

    public static void townList(CommandSender sender, String[] args) {
        if (args.length != 1) {
            return;
        }
        boolean isGenshin = false;
        if (sender instanceof Player) {
            Player player = (Player) sender;
            String uuid = player.getUniqueId().toString();
            TownPlayerDO townPlayer = TownPlayerManage.selectByUuid(uuid);
            if (townPlayer.getIdentity() == PlayerTownIdentity.GENSHIN) {
                isGenshin = true;
            }
        }
        List<TownDO> townList = TownManage.selectList(null);
        TextComponent.Builder ret = Component.text().append(Component.text().content(TOWN_LIST_TITLE).build());
        for (TownDO townDO : townList) {
            ret.appendNewline()
                    .append(Component.text(String.format(TOWN_LIST_LINE, townDO.getName())))
                    .append(Component.text(DETAIL_BUTTON)
                            .clickEvent(ClickEvent.runCommand("/poitown townInfo " + townDO.getUuid()))
                            .hoverEvent(HoverEvent.showText(Component.text(DETAIL_BUTTON)))
                    );
            if (isGenshin) {
                ret.append(Component.text(APPLY_BUTTON)
                        .clickEvent(ClickEvent.runCommand("/poitown applyJoin " + townDO.getName()))
                        .hoverEvent(HoverEvent.showText(Component.text(APPLY_BUTTON)))
                );
            }
        }
        sender.sendMessage(ret.build());
    }
}
